//index.js
//获取应用实例
const app = getApp()
var CusBase64 = require('../../utils/base64.js');  

Page({
  data: {
    dateStart: '',
    timeStart: '',
    dateEnd: '',
    timeEnd: '',
    src: '',
    title:'',
    desc:'',
    isShowAhturoizeWarning: false
  },
  onLoad: function (e) {
    var that = this
    app.globalData.orderId = e.id
    console.log(app.globalData.orderId)

    this.login();
    var now = new Date()
    var year = now.getFullYear()
    var month = now.getMonth() + 1
    var day = now.getDate() 
    var hour = now.getHours()
    var minute = now.getMinutes()
    this.setData({
      dateStart: [year, month, day].map(formatNumber).join('-'),
      timeStart: [hour, minute].map(formatNumber).join(':'),
      dateEnd: [year, month, day].map(formatNumber).join('-'),
      timeEnd: [hour, minute].map(formatNumber).join(':')
    })

  }, 


  login: function() {
    var that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              getApp().globalData.isShowAhturoizeWarning = false
              that.setData({
                isShowAhturoizeWarning: false
              })
              wx.login({
                success: res => {
                  wx.request({
                    url: getApp().data.service + '/menu/login',
                    data: {
                      code: res.code,
                      nickName: CusBase64.CusBASE64.encoder(getApp().globalData.userInfo.nickName)
                    },
                    header: {
                      'content-type': 'application/json'
                    },
                    success: function (res) {
                      console.log(res)
                      getApp().globalData.userid = res.data.data.id
                      // console.log(e.id)
                      // getApp().globalData.orderId = e.id
                      getApp().globalData.realName = CusBase64.CusBASE64.decoder(res.data.data.realName)
                      console.log(app.globalData)
                      if (app.globalData.orderId) {
                        wx.navigateTo({
                          url: '../share/share?id=' + app.globalData.orderId,
                        })
                      }
                    }
                  })
                }
              })
            }
          })
        }
        else {
          getApp().globalData.isShowAhturoizeWarning = true
          that.setData({
            isShowAhturoizeWarning: true
          })
        }
      }
    })
  },

  onShow: function (){
    this.setData({
      isShowAhturoizeWarning :getApp().globalData.isShowAhturoizeWarning
    })
  },

  inputTitle: function(e){
    this.setData({
      title:e.detail.value
    })
  },

  inputDetail: function(e){
    this.setData({
      desc:e.detail.value
    })
  },

  gotoShow: function () {
    var _this = this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        console.log(res)
        _this.setData({
          src: res.tempFilePaths
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dateStart: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      timeStart: e.detail.value
    })
  },

  bindEndDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dateEnd: e.detail.value
    })
  },
  bindEndTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      timeEnd: e.detail.value
    })
  },

  confirmAction: function() {
    console.log(this.data.dateStart)
    var stringTime1 = this.data.dateStart
   + ' '+ this.data.timeStart;
    console.log(stringTime1)
    stringTime1 = stringTime1.replace(/-/g, '/');
    var timestamp1 = Date.parse(new Date(stringTime1));
    console.log(timestamp1)

    var stringTime2 = this.data.dateEnd
      + ' ' + this.data.timeEnd;
    stringTime2 = stringTime2.replace(/-/g, '/');
    var timestamp2 = Date.parse(new Date(stringTime2));
    var title = CusBase64.CusBASE64.encoder(this.data.title)
    var desc = CusBase64.CusBASE64.encoder(this.data.desc)
    if(title.length == 0)
    {
      wx.showToast({
        title: '请填写标题',
        icon:'none'
      })
      return
    }
    if (desc.length == 0) {
      wx.showToast({
        title: '请填写详情',
        icon: 'none'
      })
      return
    }

    console.log(getApp().globalData.userid, desc, title, timestamp1, timestamp2)
    wx.request({
      url: getApp().data.service+'/menu/createMenu',
      data: {
        title: title,
        desc: desc,
        userId: getApp().globalData.userid,
        startTime: timestamp1,
        endTime: timestamp2,
        url:'url'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        wx.switchTab({
          url: '../me/me',
        })
      }
    })
  },

  bindGetUserInfo: function (e) {
    var that = this
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      getApp().globalData.userInfo = e.detail.userInfo
      wx.login({
        success: res => {
          wx.request({
            url: getApp().data.service + '/menu/login',
            data: {
              code: res.code,
              nickName: getApp().globalData.userInfo.nickName
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res)
              getApp().globalData.userid = res.data.data.id
              getApp().globalData.realName = CusBase64.CusBASE64.decoder(res.data.data.realName)
              getApp().globalData.isShowAhturoizeWarning = false
              that.setData({
                isShowAhturoizeWarning: false
              })
              if (app.globalData.orderId) {
                wx.navigateTo({
                  url: '../share/share?id=' + app.globalData.orderId,
                })
              }
              console.log(that.data.isShowAhturoizeWarning)
            }
          })
        }
      })
    } else {
      //用户按了拒绝按钮
    }
  } 
})

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
